package com.starnet.yarnmonitor.yarn.conf;

import com.zaxxer.hikari.HikariDataSource;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
//@Log4j
//@Component
//@AllArgsConstructor
public class DatabaseInitializer {
//    private final DataSourceProperties dataSourceProperties;
//
//    @PostConstruct
//    public void init() throws SQLException {
//        String url = dataSourceProperties.getUrl();
//
//        // jdbc url最后一个 '/' 用于分割具体 schema?参数
//        int lastSplitIndex = url.lastIndexOf('/');
//        // 获取spring.datasource.url具体数据库schema前的jdbc url
//        String addressUrl = url.substring(0, lastSplitIndex);
//        // 直连数据库地址:jdbc:mysql://yourIp:port
//        HikariDataSource dataSource = new HikariDataSource();
//        dataSource.setJdbcUrl(addressUrl);
//        dataSource.setUsername(dataSourceProperties.getUsername());
//        dataSource.setPassword(dataSourceProperties.getPassword());
//        try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) {
//            statement.executeUpdate("create database if not exists `my_yarn` default character set utf8 COLLATE utf8_general_ci");
//        }
//        log.info("Database initialed");
//
//
//    }
}
